﻿using AzureFileDepot.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SubscribingConsole
{
    class Program
    {
        static ServiceBusTopic topic;

        static void Main(string[] args)
        {
            topic = new ServiceBusTopic(Properties.Settings.Default.ServiceBusAccessSig, "filepublication");
            topic.CreateSubscription(Properties.Settings.Default.SubscriberName);

            //TODO: we'll save this for vNext ;)
            //topic.StartMessagePump();

            while (true)
            {
                string blobPath = topic.GetMessage();
                if (blobPath != null)
                {
                    Console.WriteLine(string.Format("New File: {0}", blobPath));

                    Uri blobURI = new Uri(blobPath);
                    string fileName = blobURI.Segments[blobURI.Segments.Length - 1];

                    // create new file and lock
                    Stream saveFile = new FileStream(Properties.Settings.Default.SaveLocation + "/" + fileName, FileMode.OpenOrCreate);
                    AzureBlob.downloadFromBlob(new Uri(blobPath), saveFile);
                    saveFile.Close();
                }
            }
        }
    }
}
